<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Member;
use Hyperf\DbConnection\Db;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/member")]
class MemberController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }
    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        //门店ID
        $store_id = $this->request->input('store_id', '');
        if ($store_id != '') {
            $where[] = ['store_id', '=', $store_id];
        }
        //用户ID
        $user_id = $this->request->input('user_id', '');
        if ($user_id != '') {
            $where[] = ['user_id', '=', $user_id];
        }
        $lists = Member::where($where)
            ->orderBy('member_id', 'DESC')
            ->paginate()->toArray();
        return compact('lists');
    }

    #[Api]
    #[GetMapping("cancelorder")]
    function cancelorder(int $order_id = 0)
    {
        $order_id = $this->request->input('order_id', '');
        $order = Order::updateOrCreate(['order_id' => $order_id], [
            'status' => 4,
            'updated_at' => time()
        ]);
        return $order ? $this->returnSuccessJson(compact('order')) : $this->returnErrorJson();
    }

    #[Api]
    #[GetMapping("refundorder")]
    function refundorder()
    {
        $order_id = $this->request->input('order_id', '');
        Db::beginTransaction();
        try {
            //退款开始

            //退款结束
            //退款成功修改订单状态
            $order = Order::updateOrCreate(['order_id' => $order_id], [
                'status' => 3,
                'updated_at' => time()
            ]);
            Db::commit();
        } catch (\Throwable $ex) {
            Db::rollBack();
        }
        return $order ? $this->returnSuccessJson(compact('order')) : $this->returnErrorJson($ex->getMessage());
    }
}
